package client

import (
	"github.com/akirakazu/go-simplejson"
	"github.com/nu7hatch/gouuid"
)

type Pack struct {
	Id  string
	Cmd string
	Msg *json.Json
}

func (p *Pack) MarshalJSON() ([]byte, error) {
	i := json.New()
	i.Set("id", p.Id)
	i.Set("cmd", p.Cmd)
	i.Set("msg", p.Msg)
	return i.MarshalJSON()
}

func NewPack(Cmd string, Msg *json.Json) *Pack {
	p := new(Pack)
	uid, _ := uuid.NewV4()
	p.Id = uid.String()
	p.Cmd = Cmd
	p.Msg = Msg
	return p
}

func NewPackWithId(Id string, Cmd string, Msg *json.Json) *Pack {
	p := new(Pack)
	p.Id = Id
	p.Cmd = Cmd
	p.Msg = Msg
	return p
}

func NewPackWithJSON(m *json.Json) *Pack {
	id, _ := m.Get("id").String()
	cmd, _ := m.Get("cmd").String()
	return NewPackWithId(id, cmd, m.Get("msg"))
}
